﻿using System;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YArchitech.HCLoad.Utils
{
	internal class HcUnit
	{
		public static double CovertFromAPI(DisplayUnitType to, double value)
		{
            if ((int)to == 56)
			{
				return value * HcUnit.ImperialDutRatio(to) - 459.67;
			}
            if ((int)to != 57)
			{
				return value *= HcUnit.ImperialDutRatio(to);
			}
			return value - 273.15;
		}

		public static double CovertToAPI(double value, DisplayUnitType from)
		{
            if ((int)from == 56)
			{
				return (value + 459.67) / HcUnit.ImperialDutRatio(from);
			}
            if ((int)from != 57)
			{
				return value /= HcUnit.ImperialDutRatio(from);
			}
			return value + 273.15;
		}

		public static double LengthConverFromAPI(Document document, double dLength)
		{
			UnitType unitType = (UnitType)0;
			return HcUnit.CovertFromAPI(UnifiedModified.GetDisplayUnitType(document, unitType), dLength);
		}

		public static double LengthConverToAPI(Document document, double dLength)
		{
			UnitType unitType = (UnitType)0;
			return HcUnit.CovertToAPI(dLength, UnifiedModified.GetDisplayUnitType(document, unitType));
		}

		public static double AreaConverFromAPI(Document document, double dArea)
		{
			UnitType unitType = (UnitType)1;
			return HcUnit.CovertFromAPI(UnifiedModified.GetDisplayUnitType(document, unitType), dArea);
		}

		public static double AreaConverToAPI(Document document, double dArea)
		{
			UnitType unitType = (UnitType)1;
			return HcUnit.CovertToAPI(dArea, UnifiedModified.GetDisplayUnitType(document, unitType));
		}

		public static double VolumeConverFromAPI(Document document, double dVolume)
		{
			UnitType unitType = (UnitType)2;
			return HcUnit.CovertFromAPI(UnifiedModified.GetDisplayUnitType(document, unitType), dVolume);
		}

		public static double AngleConverFromAPI(Document document, double dAngle)
		{
			UnitType unitType = (UnitType)3;
			return HcUnit.CovertFromAPI(UnifiedModified.GetDisplayUnitType(document, unitType), dAngle);
		}

		public static string GetUnitString(DisplayUnitType dut)
		{
			dut.ToString();
            switch ((int)dut)
			{
			case 0:
				return "Meters";
			case 1:
				return "Centimeter";
			case 2:
				return "Millimeters";
			case 3:
				return "DecimalFeet";
			case 4:
				return "FeetFractionalInches";
			case 5:
				return "FractionalInches";
			case 6:
				return "DecimalInches";
			case 7:
				return "Acres";
			case 8:
				return "Hectares";
			case 9:
				return "MetersCentimeters";
			case 10:
				return "CubicYards";
			case 11:
				return "SquareFeet";
			case 12:
				return "SquareMeterS";
			case 13:
				return "CubicFeet";
			case 14:
				return "CubicMeters";
			case 15:
				return "DecimalDegrees";
			case 16:
				return "DegreesAndMinutes";
			case 17:
				return "General";
			case 18:
				return "Fixed";
			case 19:
				return "Percentage";
			case 20:
				return "SquareInches";
			case 21:
				return "SquareCentimeters";
			case 22:
				return "SquareMillimeters";
			case 23:
				return "CubicInches";
			case 24:
				return "CubicCentimeters";
			case 25:
				return "CubicMillimeters";
			case 26:
				return "Liters";
			case 27:
				return "GallonsUs";
			case 28:
				return "KilogramsPerCubicMeter";
			case 29:
				return "PoundsMassPerCubicFoot";
			case 30:
				return "PoundsMassPerCubicInch";
			case 31:
				return "BritishThermalUnits";
			case 32:
				return "Calories";
			case 33:
				return "Kilocalories";
			case 34:
				return "Joules";
			case 35:
				return "KilowattHours";
			case 36:
				return "Therms";
			case 37:
				return "InchesOfWaterPer100ft";
			case 38:
				return "PascalsPerMeter";
			case 39:
				return "Watts";
			case 40:
				return "Kilowatts";
			case 41:
				return "BritishThermalUnitsPerSecond";
			case 42:
				return "BritishThermalUnitsPerHour";
			case 43:
				return "CaloriesPerSecond";
			case 44:
				return "KilocaloriesPerSecond";
			case 45:
				return "WattsPerSquareFoot";
			case 46:
				return "WattsPerSqareMeter";
			case 47:
				return "InchesOfWater";
			case 48:
				return "Pascals";
			case 49:
				return "Kilopascals";
			case 50:
				return "Megapascals";
			case 51:
				return "PoundsForcePerSquareInch";
			case 52:
				return "InchesOfMercury";
			case 53:
				return "MillimetersOfMercury";
			case 54:
				return "atmospheres";
			case 55:
				return "Bars";
			case 56:
				return "Fahrenheit";
			case 57:
				return "C";
			case 58:
				return "Kelvin";
			case 59:
				return "Rankine";
			case 60:
				return "FeetPerMinute";
			case 61:
				return "MetersPerSecond";
			case 62:
				return "CentimetersPerMinute";
			case 63:
				return "CubicFeetPerMinute";
			case 64:
				return "LitersPerSecond";
			case 65:
				return "CubicMetersPerSecond";
			case 66:
				return "CubicMetersPerHour";
			case 67:
				return "GallonsUsPerMinute";
			case 68:
				return "GallonsUsPerHour";
			case 69:
				return "Amperes";
			case 70:
				return "Kiloamperes";
			case 71:
				return "Milliamperes";
			case 72:
				return "Volts";
			case 73:
				return "Kilovolts";
			case 74:
				return "Millivolts";
			case 75:
				return "Hertz";
			case 76:
				return "CyclesPerSecond";
			case 77:
				return "Lux";
			case 78:
				return "Footcandles";
			case 79:
				return "Footlamberts";
			case 80:
				return "CandelasPerSquareMeter";
			case 81:
				return "Candelas";
			case 82:
				return "Candlepower";
			case 83:
				return "Lumens";
			case 84:
				return "VoltAmperes";
			case 85:
				return "KilovoltAmperes";
			case 86:
				return "Horsepower";
			case 87:
				return "Newtons";
			case 88:
				return "Decanewtons";
			case 89:
				return "Kilonewtons";
			case 90:
				return "Meganewtons";
			case 91:
				return "Kips";
			case 92:
				return "KilogramsForce";
			case 93:
				return "TonnesForce";
			case 94:
				return "PoundsForce";
			case 95:
				return "NewtonsPerMeter";
			case 96:
				return "DecaNewtonsPerMeter";
			case 97:
				return "KilonewtonsPerMeter";
			case 98:
				return "MeganewtonsPerMeter";
			case 99:
				return "KipsPerFoot";
			case 100:
				return "KilogramsForcePerMeter";
			case 101:
				return "TonnesForcePerMeter";
			case 102:
				return "PoundsForcePerFooT";
			case 103:
				return "NewtonsPerSqareMeter";
			case 104:
				return "DecaNewtonsPerSqareMeter";
			case 105:
				return "KilonewtonsPerSqareMeter";
			case 106:
				return "MeganewtonsPerSqareMeter";
			case 107:
				return "KipsPerSquareFoot";
			case 108:
				return "KilogramsForcePerSqareMeter";
			case 109:
				return "TonnesForcePerSqareMeter";
			case 110:
				return "PoundsForcePerSquareFoot";
			case 111:
				return "NewtonMeters";
			case 112:
				return " DecaNewtonMeters";
			case 113:
				return "KilonewtonMeters";
			case 114:
				return "MeganewtonMeters";
			case 115:
				return "KipFeet";
			case 116:
				return "KilogramForceMeters";
			case 117:
				return "TonneForceMeters";
			case 118:
				return "PoundForceFeet";
			case 119:
				return "MetersPerKilonewton";
			case 120:
				return "FeetPerKip";
			case 121:
				return "SqareMetersPerKilonewton";
			case 122:
				return "SquareFeetPerKip";
			case 123:
				return "CubicMetersPerKilonewton";
			case 124:
				return "CubicFeetPerKip";
			case 125:
				return "InvKilonewtons";
			case 126:
				return "InvKips";
			case 127:
				return "FeetOfWaterPer100ft";
			case 128:
				return "FeetOfWater";
			case 129:
				return "PascalSeconds";
			case 130:
				return "PoundsMassPerFootSecond";
			case 131:
				return "Centipoises";
			case 132:
				return "FeetPerSecond";
			case 133:
				return "KipsPerSquareInch";
			case 134:
				return "KilonewtonsPerCubicMeter";
			case 135:
				return "PoundsForcePerCubicFoot";
			case 136:
				return "KipsPerCubicInch";
			case 137:
				return "InvFahrenheit";
			case 138:
				return "InvCelsius";
			case 139:
				return "NewtonMetersPerMeter";
			case 140:
				return " DecaNewtonMetersPerMeter";
			case 141:
				return "KilonewtonMetersPerMeter";
			case 142:
				return "MeganewtonMetersPerMeter";
			case 143:
				return "KipFeetPerFoot";
			case 144:
				return "KilogramForceMetersPerMeter";
			case 145:
				return "TonneForceMetersPerMeter";
			case 146:
				return "PoundForceFeetPerFoot";
			case 147:
				return "PoundsMassPerFootHour";
			case 148:
				return "KipsPerInch";
			case 149:
				return "KipsPerCubicFoot";
			case 150:
				return "KipFeetPerDegree";
			case 151:
				return "KilonewtonMetersPerDegree";
			case 152:
				return "KipFeetPerDegreePerFoot";
			case 153:
				return "KilonewtonMetersPerDegreepermeter";
			default:
				return "";
			}
		}

		private static double ImperialDutRatio(DisplayUnitType dut)
		{
            switch ((int)dut)
			{
			case 0:
				return 0.3048;
			case 1:
				return 30.48;
			case 2:
				return 304.8;
			case 3:
				return 1.0;
			case 4:
				return 1.0;
			case 5:
				return 12.0;
			case 6:
				return 12.0;
			case 7:
				return 2.29568411386593E-05;
			case 8:
				return 9.290304E-06;
			case 9:
				return 0.3048;
			case 10:
				return 0.037037037037037;
			case 11:
				return 1.0;
			case 12:
				return 0.09290304;
			case 13:
				return 1.0;
			case 14:
				return 0.028316846592;
			case 15:
				return 57.2957795130823;
			case 16:
				return 57.2957795130823;
			case 17:
				return 1.0;
			case 18:
				return 1.0;
			case 19:
				return 100.0;
			case 20:
				return 144.0;
			case 21:
				return 929.0304;
			case 22:
				return 92903.04;
			case 23:
				return 1728.0;
			case 24:
				return 28316.846592;
			case 25:
				return 28316846.592;
			case 26:
				return 28.316846592;
			case 27:
				return 7.48051905367236;
			case 28:
				return 35.3146667214886;
			case 29:
				return 2.20462262184878;
			case 30:
				return 0.00127582327653286;
			case 31:
				return 8.80550918411529E-05;
			case 32:
				return 0.0221895098882201;
			case 33:
				return 2.21895098882201E-05;
			case 34:
				return 0.09290304;
			case 35:
				return 2.58064E-08;
			case 36:
				return 8.80547457016663E-10;
			case 37:
				return 1.31845358262865;
			case 38:
				return 10.7639104167097;
			case 39:
				return 0.09290304;
			case 40:
				return 9.290304E-05;
			case 41:
				return 8.80550918411529E-05;
			case 42:
				return 0.316998330628151;
			case 43:
				return 0.0221895098882201;
			case 44:
				return 2.21895098882201E-05;
			case 45:
				return 0.09290304;
			case 46:
				return 1.0;
			case 47:
				return 0.0131845358262865;
			case 48:
				return 3.28083989501312;
			case 49:
				return 0.00328083989501312;
			case 50:
				return 3.28083989501312E-06;
			case 51:
				return 0.000475845616460903;
			case 52:
				return 0.000968831370233344;
			case 53:
				return 0.0246083170946002;
			case 54:
				return 3.23793722675857E-05;
			case 55:
				return 3.28083989501312E-05;
			case 56:
				return 1.8;
			case 57:
				return 1.0;
			case 58:
				return 1.0;
			case 59:
				return 1.8;
			case 60:
				return 60.0;
			case 61:
				return 0.3048;
			case 62:
				return 1828.8;
			case 63:
				return 60.0;
			case 64:
				return 28.316846592;
			case 65:
				return 0.028316846592;
			case 66:
				return 101.9406477312;
			case 67:
				return 448.831143220342;
			case 68:
				return 26929.8685932205;
			case 69:
				return 1.0;
			case 70:
				return 0.001;
			case 71:
				return 1000.0;
			case 72:
				return 0.09290304;
			case 73:
				return 9.290304E-05;
			case 74:
				return 92.90304;
			case 75:
				return 1.0;
			case 76:
				return 1.0;
			case 77:
				return 10.7639104167097;
			case 78:
				return 1.0000000387136;
			case 79:
				return 3.1415927449471;
			case 80:
				return 10.7639104167097;
			case 81:
				return 1.0;
			case 82:
				return 1.0;
			case 83:
				return 1.0;
			case 84:
				return 0.09290304;
			case 85:
				return 9.290304E-05;
			case 86:
				return 0.00012458502883053;
			case 87:
				return 0.3048;
			case 88:
				return 0.03048;
			case 89:
				return 0.0003048;
			case 90:
				return 3.048E-07;
			case 91:
				return 0.224808943099711;
			case 92:
				return 0.0310810655372411;
			case 93:
				return 3.10810655372411E-05;
			case 94:
				return 224.80894309971;
			case 95:
				return 1.0;
			case 96:
				return 0.1;
			case 97:
				return 0.001;
			case 98:
				return 1E-06;
			case 99:
				return 6.85217658567918E-05;
			case 100:
				return 0.101971999794098;
			case 101:
				return 0.000101971999794098;
			case 102:
				return 0.0685217658567918;
			case 103:
				return 3.28083989501312;
			case 104:
				return 0.328083989501312;
			case 105:
				return 0.00328083989501312;
			case 106:
				return 3.28083989501312E-06;
			case 107:
				return 6.85217658567918E-05;
			case 108:
				return 0.334553805098747;
			case 109:
				return 0.000334553805098747;
			case 110:
				return 0.0685217658567917;
			case 111:
				return 0.09290304;
			case 112:
				return 0.009290304;
			case 113:
				return 9.290304E-05;
			case 114:
				return 9.290304E-08;
			case 115:
				return 6.85217658567918E-05;
			case 116:
				return 0.00947350877575109;
			case 117:
				return 9.47350877575109E-06;
			case 118:
				return 0.0685217658567918;
			case 119:
				return 1000.0;
			case 120:
				return 14593.9029372064;
			case 121:
				return 304.8;
			case 122:
				return 14593.9029372064;
			case 123:
				return 92.90304;
			case 124:
				return 14593.9029372064;
			case 125:
				return 3280.83989501312;
			case 126:
				return 14593.9029372064;
			case 127:
				return 0.109761336731934;
			case 128:
				return 0.00109764531546318;
			case 129:
				return 3.28083989501312;
			case 130:
				return 2.20462262184878;
			case 131:
				return 3280.83989501312;
			case 132:
				return 1.0;
			case 133:
				return 4.75845596227721E-07;
			case 134:
				return 0.0107639104167097;
			case 135:
				return 0.0685217658567918;
			case 136:
				return 3.96537996856434E-08;
			case 137:
				return 0.555555555555556;
			case 138:
				return 1.0;
			case 139:
				return 0.3048;
			case 140:
				return 0.03048;
			case 141:
				return 0.0003048;
			case 142:
				return 3.048E-07;
			case 143:
				return 6.85217658567918E-05;
			case 144:
				return 0.0310810655372411;
			case 145:
				return 3.10810655372411E-05;
			case 146:
				return 0.0685217658567918;
			case 147:
				return 7936.64143865559;
			case 148:
				return 5.71014715473265E-06;
			case 149:
				return 6.85217658567918E-05;
			case 150:
				return 2.08854342331501E-05;
			case 151:
				return 9.290304E-05;
			case 152:
				return 2.08854342331501E-05;
			case 153:
				return 0.0003048;
			default:
				return 1.0;
			}
		}
	}
}
